/*
** cmd.c for  in /home/faye_m//zappy
** 
** Made by mouhamed faye
** Login   <faye_m@epitech.net>
** 
** Started on  Mon Jun 20 20:51:43 2011 mouhamed faye
** Last update Fri Jul  1 14:06:13 2011 mouhamed faye
*/
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cmd.h"
#include "team.h"
#include "time.h"
#include "player.h"

void	manage_cmd(Vars_Packages *var)
{
  char  *tmp;
  tmp = malloc(sizeof(*tmp));
  tmp = rindex(var->buffer, '\n');
  tmp[0] = '\0';
  printf ("%d ",  get_time());
  printf ("Received MSG from %d : %s\n", var->sockfd2, var->buffer);

  if (strncmp (var->buffer, "GRAPHIC", 7) == 0)
    cmd_graph(var);
  else
    cmd_text(var);
}

int     cmd_add_player(Vars_Packages *var)                                                                                   
{ 
  Class_team    *tmp = malloc(sizeof(*tmp));  
  tmp = var->list_of_team;
  while (tmp->next != NULL)
    {
      if (strcmp(tmp->team_name, var->buffer) == 0)
	{
	  tmp->list_player = add_player(tmp->list_player, var->sockfd2);
	  printf ("\tSending OK to ...... %d\n", var->sockfd2);
	  write(var->sockfd2, "OK\n", 3);
	  return 1; 
        }  
      tmp = tmp->next;
    }                                                                                                                                                      
  printf ("\tSending KO to ...... %d\n", var->sockfd2);
  /* disconnect(var); wait to disconnect : test if nb_req = 1*/ 
  return(0);
}
int	cmd_add_request(Vars_Packages *var)
{
 printf ("fonction cmd_add-request to implement\n");
return(0);
}

void   cmd_analyzer(Vars_Packages *var)
{
  if (cmd_add_player(var))
    printf ("MAP: sending Positon %d\n", var->sockfd2);
  else if (cmd_add_request(var))
    printf ("Send ok func on team \n");
  else
     printf ("Bad request \n");
}
         
void	cmd_graph(Vars_Packages *var)
{
  /* call function map of tessa_b  TO GET THE MAP */
  int i = 0;
  for (i = 0; i < 10 ; i++)
    write(var->sockfd2, "0 0 0 0 0 0 0 0\n", 13);
}

void	cmd_text(Vars_Packages *var)
{
  cmd_analyzer(var);
}
/*
list_request	*add_request(list_request *req, char *cmd)
{
  list_request *updated_list; 
  updated_list = malloc(sizeof(list_request));
  if(!updated_list)
    printf ("fonction add_command segfault segfault the zappy ....\n");
  updated_list->cmd = cmd;
  updated_list->delay = 100;
  updated_list->num_client  = 1;
  updated_list->nb_req +=1;
  updated_list->next = NULL;
  if (req == NULL)
    return (updated_list);
  else
    {
      list_request *tmp = req; 
      while (tmp->next != NULL)
	{
	  tmp = tmp->next;
	}
      tmp->next = updated_list;
      return(req);
    }
}

list_request	*del_request(list_request *req, char *cmd)
{
  if (req == NULL)
    return NULL;
  
  if (req != NULL)
    {
      list_request *to_return =  req->next;
      free(req);
      return (to_return);
    }
  else
    {
      return (NULL);
    }
}

void	display_request(list_request *all_request)
{
  list_request		*tmp = all_request;
  int			i;
  i = 0;
    while (tmp != NULL)
    {
      printf ("cmd n: %d : %s\n", i++, tmp->cmd);
      tmp = tmp->next;
    }
}
*/
